﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Collections.Specialized;
using StateGrid.GlobalVariables;
using Microsoft.Office.Interop.Word;

namespace StateGrid
{
    /// <summary>
    /// Summary description for GlobalMethod
    /// </summary>
    public class GlobalMethod
    {
        /// <summary>
        /// 更正半角分号
        /// </summary>
        /// <param name="str">返回更正后的结果</param>
        /// <returns></returns>
        public static String Semicolon(String str)
        {
            if(null==str||"".Equals(str))
            {
                return str;
            }

            char[] replaceChar = { ',', '，', '；', ':', '：', '?', '？' };
            //String[] replaceChar = { ",", "，", "；", ":", "：","?","？" };
            //逐一替换非法字符
            foreach(char c in replaceChar)
            {
                str=str.Replace(c,';');
            }

            return str;
        }

        /// <summary>
        /// 跳转到登录页
        /// </summary>
        public static void RedirectLogin(HttpResponse Response)
        {
            //Response.Redirect(GlobalVariables.GlobalResource.LOGIN_PAGE);
            Response.Write(" <script> top.location = '" + GlobalResource.LOGIN_PAGE + "' </script> ");
            return;
        }

        /// <summary>
        /// 将Word文档转化成PDF文档
        /// </summary>
        /// <param name="docFileName">要转化的DOC文件（包含路径及其文件名）</param>
        /// <param name="pdfFileName">转化后的PDF文件（包含路径及其文件名）</param>
        /// <returns></returns>
        public static bool ConvertDocToPDF(string docFileName, string pdfFileName)
        {
            String docFilePath = OperationFile.FormatPath(docFileName);
            String pdfFilePath = OperationFile.FormatPath(pdfFileName);

            WdExportFormat exportFormat=WdExportFormat.wdExportFormatPDF;
            bool result;
            object paramMissing = Type.Missing;
            ApplicationClass wordApplication = new ApplicationClass();
            Document wordDocument = null;
            try
            {
                object paramSourceDocPath = docFilePath+docFileName;
                string paramExportFilePath = pdfFilePath+pdfFileName;

                WdExportFormat paramExportFormat = exportFormat;
                bool paramOpenAfterExport = false;
                WdExportOptimizeFor paramExportOptimizeFor =
                WdExportOptimizeFor.wdExportOptimizeForPrint;
                WdExportRange paramExportRange = WdExportRange.wdExportAllDocument;
                int paramStartPage = 0;
                int paramEndPage = 0;
                WdExportItem paramExportItem = WdExportItem.wdExportDocumentContent;
                bool paramIncludeDocProps = true;
                bool paramKeepIRM = true;
                WdExportCreateBookmarks paramCreateBookmarks = WdExportCreateBookmarks.wdExportCreateWordBookmarks;
                bool paramDocStructureTags = true;
                bool paramBitmapMissingFonts = true;
                bool paramUseISO19005_1 = false;

                wordDocument = wordApplication.Documents.Open(
                        ref paramSourceDocPath, ref paramMissing, ref paramMissing,
                        ref paramMissing, ref paramMissing, ref paramMissing,
                        ref paramMissing, ref paramMissing, ref paramMissing,
                        ref paramMissing, ref paramMissing, ref paramMissing,
                        ref paramMissing, ref paramMissing, ref paramMissing,
                        ref paramMissing);

                if (wordDocument != null)
                    wordDocument.ExportAsFixedFormat(paramExportFilePath,
                            paramExportFormat, paramOpenAfterExport,
                            paramExportOptimizeFor, paramExportRange, paramStartPage,
                            paramEndPage, paramExportItem, paramIncludeDocProps,
                            paramKeepIRM, paramCreateBookmarks, paramDocStructureTags,
                            paramBitmapMissingFonts, paramUseISO19005_1,
                            ref paramMissing);
                result = true;
            }
            catch (Exception)
            {
                return false;
            }
            finally
            {
                if (wordDocument != null)
                {
                    wordDocument.Close(ref paramMissing, ref paramMissing, ref paramMissing);
                    wordDocument = null;
                }
                if (wordApplication != null)
                {
                    wordApplication.Quit(ref paramMissing, ref paramMissing, ref paramMissing);
                    wordApplication = null;
                }
                GC.Collect();
                GC.WaitForPendingFinalizers();
                GC.Collect();
                GC.WaitForPendingFinalizers();
            }
            return result;
        }
    }
}